package com.xuanwei.schedule.config;

import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.xuanwei.dao.entity.XwWeixinPayConfig;
import com.xuanwei.dao.mapper.XwWeixinPayConfigMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author jiangyong
 * @Description 微信支付配置
 * @Date 2019/7/12 12:22
 **/
@Configuration
public class WxPayConfig {


    private XwWeixinPayConfigMapper weixinPayConfigMapper;

    public WxPayConfig(XwWeixinPayConfigMapper weixinPayConfigMapper) {
        this.weixinPayConfigMapper = weixinPayConfigMapper;
    }

    @Bean
    public WxPayService wxPayService() {

        com.github.binarywang.wxpay.config.WxPayConfig config = new com.github.binarywang.wxpay.config.WxPayConfig();

        XwWeixinPayConfig payConfig = weixinPayConfigMapper.selectById(1);

        if (payConfig == null) {
            throw new RuntimeException("微信支付配置文件不存在");
        }

        config.setAppId(payConfig.getAppId());
        config.setMchId(payConfig.getMchId());
        config.setNotifyUrl(payConfig.getNotifyUrl());
        config.setKeyContent(payConfig.getApiclientCertP12());
        config.setMchKey(payConfig.getApiKey());
        config.setSignType("MD5");
        config.setTradeType("JSAPI");


        WxPayServiceImpl payService = new WxPayServiceImpl();
        payService.setConfig(config);
        return payService;
    }
}
